{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Learning rate decay scheduling\n",
    "\n",
    "This notebook is not specific to Distiller.\n",
    "\n",
    "When fine-tuning or training a model, you may want to try different LR-decay policies.  This notebook shows how the different policies work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import widgets, interact\n",
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "from torch.optim.lr_scheduler import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchvision\n",
    "model = torchvision.models.alexnet(pretrained=True).cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@interact(first_epoch=(0,100), last_epoch=(1,100), step_size=(1, 30, 1), gamma=(0, 1, 0.05), lr='0.001', T_max=(1,10),\n",
    "         enable_steplr=True, \n",
    "         enable_explr=True,\n",
    "         enable_cosinelr=False,\n",
    "         enable_multisteplr=True)\n",
    "def draw_schedules(first_epoch=0, last_epoch=50, step_size=3, gamma=0.9, lr=0.001, T_max=1, \n",
    "                   enable_steplr=True,\n",
    "                   enable_explr=True,\n",
    "                   enable_cosinelr=False,\n",
    "                   enable_multisteplr=True):\n",
    "    lr = float(lr)\n",
    "    optimizer = torch.optim.SGD(model.parameters(), lr, momentum=0.9, weight_decay=0.0001)\n",
    "\n",
    "    schedulers = {}\n",
    "    if enable_explr:\n",
    "        schedulers['ExponentialLR'] = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma)\n",
    "    if enable_steplr:\n",
    "        schedulers['StepLR'] = torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma)\n",
    "    if enable_cosinelr:\n",
    "        schedulers['CosineAnnealingLR'] = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max)\n",
    "    if enable_multisteplr:\n",
    "        schedulers['MultiStepLR'] = MultiStepLR(optimizer, milestones=[30,80], gamma=gamma)\n",
    "    \n",
    "    epochs = []\n",
    "    lr_values = {}\n",
    "    for name in schedulers.keys():\n",
    "        lr_values[name] = []\n",
    "\n",
    "    for epoch in range(first_epoch, last_epoch):\n",
    "        epochs.append(epoch)\n",
    "        for name, scheduler in schedulers.items():\n",
    "            scheduler.step(epoch)\n",
    "            lr = scheduler.get_lr()\n",
    "            lr_values[name].append(lr)    \n",
    "\n",
    "    for name in schedulers.keys():\n",
    "        plt.plot(epochs, lr_values[name])\n",
    "    plt.ylabel('LR')\n",
    "    plt.xlabel('epoch')\n",
    "    plt.title('Learning Rate Schedulers')\n",
    "    plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# References\n",
    "\n",
    " 1. <div id=\"pytorch1\"></div> **http://pytorch.org/docs/master/_modules/torch/optim/lr_scheduler.html**\n",
    " 2. <div id=\"pytorch1\"></div> **http://pytorch.org/docs/master/optim.html#how-to-adjust-learning-rate** "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
